﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using QCM.Forms.Properties;
using QCM.Forms.ServiceQcm;

namespace QCM.Forms
{
    public partial class QCMClientForm : Form
    {
        ServiceQcm.ContractClient proxy;
        Font fontTitleQuestion = null;

        public QCMClientForm()
        {
            Trace.WriteLine("Démarrage de Form1","Catégorie");
            this.fontTitleQuestion = Settings.Default.FontTitleQuestion;
            InitializeComponent();
            splitContainer1.Panel2.Enabled = false;
        }

        private void RefreshTreeView_Click(object sender, EventArgs e)
        {
            this.treeView1.Nodes.Clear();
            try
            {
                foreach (ServiceQcm.Categorie cat in proxy.GetCategories())
                {
                    TreeNode tn = new TreeNode(cat.Nom) { Name = cat.Nom };

                    if (cat.NomMere == string.Empty)
                    {
                        this.treeView1.Nodes.Add(tn);
                    }
                    else
                    {
                        TreeNode[] treeNodes = treeView1.Nodes.Find(cat.NomMere, true);
                        if (treeNodes.Length == 1)
                        {
                            treeNodes[0].Nodes.Add(tn);
                        }
                        else
                        {
                            throw new Exception("Problème de catégorie: " + cat.NomMere + ", cette categorie n'existe pas ou est en doublon");
                        }
                    }
                }
            }
                catch(System.ServiceModel.FaultException<DataSourceFaultException> exp)
            {
                Trace.Write("expection:"+ exp.Reason);

            }
        }

        private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {

        }

        private void chargerUneFontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            if (fd.ShowDialog() == DialogResult.OK)
            {
                fontTitleQuestion = fd.Font;
                foreach (Object qctrl in this.splitContainer1.Panel2.Controls)
                {
                    if(qctrl.GetType() == typeof(Control))
                        ((Control)qctrl).Font = fontTitleQuestion;
                }
            }
        }

        private void seConnecterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Trace.WriteLine("Demande de connexion au service : EN COURS");
            try
            {
                proxy = new QCM.Forms.ServiceQcm.ContractClient();
                Trace.WriteLine("Demande de connexion au service : OK");
                this.toolStripStatusLabel1.Text = "connexion au service : OK";
            }
            catch (Exception)
            {
                Trace.WriteLine("Demande de connexion au service : NOK");
                this.toolStripStatusLabel1.Text = "connexion au service : NOK";
            }
        }
    }
}
